package com.ximu.service_edu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ximu.common_utils.Result;
import com.ximu.service_edu.entity.EduTeacher;
import com.ximu.service_edu.entity.vo.TeacherQuery;
import com.ximu.service_edu.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author ximu
 * @since 2022-02-15
 */
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/dev-api/service_edu/edu_teacher")
@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    /*
     * @param :null
      * @return List<EduTeacher>
     * @author ximu
     * @description 查询所有教师
     * @date 2022/2/15 22:36
     */
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAllTeacher")
    public Result findAllTeacher(){

        List<EduTeacher> list = eduTeacherService.list(null);
        return Result.ok().data("items",list);

    }


    /*
     * @param page:
    	 * @param limit:
      * @return Result
     * @author ximu
     * @description 分页查询所有讲师
     * @date 2022/2/17 17:54
     */
    @ApiOperation(value = "分页查询所有讲师")
    @GetMapping("pageAllTeacher/{page}/{limit}")
    public  Result pageFindAllTeacher(
            @ApiParam(name = "page",value = "当前页码",required = true)
            @PathVariable long page,
            @ApiParam(name = "limit",value = "每页记录数",required = true)
            @PathVariable long limit
    ){
        //创建page对象
        Page<EduTeacher> pageParam = new Page<>(page,limit);
        Page<EduTeacher> teacherPage = eduTeacherService.page(pageParam, null);
        //总记录数
        long total = pageParam.getTotal();
        return Result.ok().data("total",total).data("teacherPage",teacherPage);
    }


    /*
     * @param id:
      * @return boolean
     * @author ximu
     * @description 删除教师
     * @date 2022/2/16 0:12
     */
    @ApiOperation(value = "根据id删除讲师")
    @DeleteMapping("removeTeacher/{id}")
    public Result removeTeacher(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id){
        boolean rs = eduTeacherService.removeById(id);
        if (rs){
            return Result.ok();
        }else {
            return Result.error();
        }

    }


    /*
     * @param page:
     * @param limit:
     * @param teacherQuery:
     * @return Result
     * @author ximu
     * @description 多条件组合分页查询
     * @date 2022/2/23 11:31
     */
    @ApiOperation(value = "多条件组合分页查询讲师")
    @PostMapping("pageTeacherCondition/{page}/{limit}")
    public Result pageTeacherCondition(@PathVariable long page, @PathVariable long limit, @RequestBody(required = false) TeacherQuery teacherQuery){
        //创建page
        Page<EduTeacher> teacherPage = new Page<>(page,limit);
        //构造条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件组合查询，动态sql，判断条件是否为空
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)){
            //eq:等于
            wrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)){
            //ge:大于等于
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            //le:小于等于
            wrapper.le("gmt_modified",end);
        }
        Page<EduTeacher> page1 = eduTeacherService.page(teacherPage, wrapper);
        //总记录数
        long total = page1.getTotal();
        return Result.ok().data("total",total).data("page1",page1);

    }


    /*
     * @param eduTeacher:
      * @return Result
     * @author ximu
     * @description 新增讲师
     * @date 2022/3/4 9:22
     */
    @ApiOperation(value = "新增讲师")
    @PostMapping("addTeacher")
    public Result addTeacher(@ApiParam(name = "teacher",value = "讲师对象",required = true) @RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if (save){
            return Result.ok();
        }else {
            return Result.error().message("新增失败");
        }

    }

    /*
     * @param id:
      * @return Result
     * @author ximu
     * @description id查询讲师
     * @date 2022/3/4 9:27
     */
    @ApiOperation(value = "id查询讲师")
    @GetMapping("getById/{id}")
    public Result getById(@ApiParam(name = "id",value = "讲师id",required = true)@PathVariable String id){
        EduTeacher byId = eduTeacherService.getById(id);
        return Result.ok().data("item",byId);
    }


    /*
     * @param id:
    	 * @param eduTeacher:
      * @return Result
     * @author ximu
     * @description 修改讲师
     * @date 2022/3/4 9:47
     */
    @ApiOperation(value = "修改讲师")
    @PutMapping("updateById/{id}")
    public Result updateById(@ApiParam(name = "id",value = "讲师id",required = true)@PathVariable String id,@ApiParam(name = "eduTeacher",value = "讲师对象",required = true)@RequestBody EduTeacher eduTeacher){

        eduTeacher.setId(id);
        boolean update = eduTeacherService.updateById(eduTeacher);
        if (update){
            return Result.ok();
        }else {
            return Result.error();
        }
    }
}

